package com.starhub.common.bean;

import java.io.Serializable;

import com.starhub.common.enumeration.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.exception.ExceptionUtils;

@ApiModel("接口统一返回类")
public class ResultResp<T> implements Serializable {

    @ApiModelProperty("状态码")
    private int code;
    @ApiModelProperty("提示信息")
    private String msg;
    @ApiModelProperty("状态")
    private boolean status;
    @ApiModelProperty("数据")
    private T data;
    @ApiModelProperty("是否fail")
    private boolean isFail;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isFail() {
        return isFail;
    }

    public void setFail(boolean fail) {
        isFail = fail;
    }


    public ResultResp(T data) {
        this.code = ResultCodeEnum.SUCCESS.getCode();
        this.msg = ResultCodeEnum.SUCCESS.getMsg();
        this.status = true;
        this.data = data;
    }

    public ResultResp(int code, String msg, T data) {
        this.status = true;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultResp(int code, String msg, T data, boolean status) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.status = status;
    }

    public ResultResp(int code, String msg, T data, boolean status, boolean isFail) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.status = status;
        this.isFail = isFail;
    }

    public static <T> ResultResp<T> success(T data) {
        return new ResultResp(data);
    }

    public static <T> ResultResp<T> success(T data, String message) {
        return new ResultResp(ResultCodeEnum.SUCCESS.getCode(), message, data);
    }

    public static <T> ResultResp<T> fail(ResultCodeEnum resultCodeEnum, boolean isBusinessError) {
        return new ResultResp(resultCodeEnum.getCode(), resultCodeEnum.getMsg(), (Object)null, false, !isBusinessError);
    }

    public static <T> ResultResp<T> fail(ResultCodeEnum resultCodeEnum, Throwable ex) {
        ResultResp<T> result = new ResultResp(resultCodeEnum.getCode(), resultCodeEnum.getMsg(), (Object)null, false, true);
        return result;
    }

    public static <T> ResultResp<T> fail(String msg) {
        return new ResultResp(ResultCodeEnum.ERROR.getCode(), msg, (Object)null, false, true);
    }

    public static <T> ResultResp<T> fail(String msg, boolean isBusinessError) {
        return new ResultResp(ResultCodeEnum.ERROR.getCode(), msg, (Object)null, false, !isBusinessError);
    }

    public static <T> ResultResp<T> fail(String msg, Throwable ex) {
        ResultResp<T> result = new ResultResp(ResultCodeEnum.ERROR.getCode(), msg, ExceptionUtils.getStackTrace(ex), false, true);
        return result;
    }


    public static <T> ResultResp<T> fail(int code, String msg) {
        return new ResultResp(code, msg, (Object)null, false, true);
    }

    public static <T> ResultResp<T> fail(int code, String msg, T data) {
        return new ResultResp(code, msg, data, false, true);
    }


}
